package com.atguigu.tingshu.user.strategy.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2024-08-23 10:32
 */
@Slf4j
@Component("1003")
public class VIPPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 处理购买商品类型为会员-发货逻辑
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]处理购买商品类型为会员发货逻辑");
        //3.处理购买项目类型-VIP会员
        //3.1 根据订单编号查询是否处理过VIP购买记录
        LambdaQueryWrapper<UserVipService> userVipServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userVipServiceLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(userVipServiceLambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        //3.2 保存本次会员购买记录
        UserVipService userVipService = new UserVipService();
        //3.2.1 获取当前用户VIP身份
        Boolean isVIP = false;
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            isVIP = true;
        }
        //3.2.2 计算本次会员开始及过期时间
        //3.2.3 获取VIP套餐信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        Date startTime = new Date();
        if (!isVIP) {
            //普通用户
            userVipService.setStartTime(startTime);
            userVipService.setExpireTime(DateUtil.offsetMonth(startTime, serviceMonth));
        } else {
            startTime = DateUtil.offsetDay(userInfo.getVipExpireTime(), 1);
            userVipService.setStartTime(startTime);
            userVipService.setExpireTime(DateUtil.offsetMonth(startTime, serviceMonth));
        }
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipServiceMapper.insert(userVipService);
        //3.3 修改用户VIP标识及过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
